/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NETWORK_MANAGER_H
#define NETWORK_MANAGER_H

#include "napi/native_api.h"
#include "network/netmanager/net_connection.h"
#include "network/netmanager/net_connection_type.h"
#include "BasicServicesKit/oh_commonevent.h"

#include "CordovaPlugin.h"
class NetworkManager: CordovaPlugin {
    const string WIFI = "wifi";
    const string WIMAX = "wimax";
    // mobile
    const string MOBILE = "mobile";
    
    // Android L calls this Cellular, because I have no idea!
    const string CELLULAR = "cellular";
    // 2G network types
    const string TWO_G = "2g";
    const string GSM = "gsm";
    const string GPRS = "gprs";
    const string EDGE = "edge";
    // 3G network types
    const string THREE_G = "3g";
    const string CDMA = "cdma";
    const string UMTS = "umts";
    const string HSPA = "hspa";
    const string HSUPA = "hsupa";
    const string HSDPA = "hsdpa";
    const string ONEXRTT = "1xrtt";
    const string EHRPD = "ehrpd";
    // 4G network types
    const string FOUR_G = "4g";
    const string LTE = "lte";
    const string UMB = "umb";
    const string HSPA_PLUS = "hspa+";
    // return type
    const string TYPE_UNKNOWN = "unknown";
    const string TYPE_ETHERNET = "ethernet";
    const string TYPE_ETHERNET_SHORT = "eth";
    const string TYPE_WIFI = "wifi";
    const string TYPE_2G = "2g";
    const string TYPE_3G = "3g";
    const string TYPE_4G = "4g";
    const string TYPE_NONE = "none";
    
    CommonEvent_SubscribeInfo* m_info;
    CommonEvent_Subscriber* m_subscriber;
    bool CreateSubscribeInfo();
    void DestroySubscribeInfo();
    
    bool CreateSubscriber();
    void DestroySubscriber();
    
    bool Subscribe();
    bool UnSubscribe();
    
    CallbackContext m_cbc;
    class CheckNetStatus :public CThread {
        void checkNetStatus(void* arg);
    public:
        void Execute(void * arg);
    };
    CheckNetStatus m_checkNetStatus;
public:
    NetworkManager(){
    }
    ~NetworkManager() {
    };
    void onMessage(const string &id, const string& data) override;
    bool execute(const string&  actionAsString, cJSON* args, CallbackContext cbc) override;
    void pluginInitialize() override;
    void getConnectionInfo();
    static void onRevice(const CommonEvent_RcvData *data);
    void updateConnectInfo(const CommonEvent_RcvData *data);
    void sendUpdate(string type);
};
#endif //MYAPPLICATION_NETWORKMANAGER_H
